<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 最远足迹（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某探险队负责对地下洞穴进行探险。探险队成员在进行探险任务时&#xff0c;随身携带的记录器会不定期地记录自身的坐标&#xff0c;但在记录的间隙中也会记录其他数据。探索工作结束后&#xff0c;探险队需要获取到某成员在探险过程中相对于探险队总部的最远的足迹位置。</p> 
<ol><li>仪器记录坐标时&#xff0c;坐标的数据格式为(x,y)&#xff0c;如(1,2)、(100,200)&#xff0c;其中0&lt;x&lt;1000&#xff0c;0&lt;y&lt;1000。同时存在非法坐标&#xff0c;如(01,1)、(1,01)&#xff0c;(0,100)属于非法坐标。</li><li>设定探险队总部的坐标为(0,0)&#xff0c;某位置相对总部的距离为&#xff1a;x*x&#43;y*y。</li><li>若两个座标的相对总部的距离相同&#xff0c;则第一次到达的坐标为最远的足迹。</li><li>若记录仪中的坐标都不合法&#xff0c;输出总部坐标&#xff08;0,0&#xff09;。</li></ol> 
<p><strong>备注&#xff1a;</strong></p> 
<p>不需要考虑双层括号嵌套的情况&#xff0c;比如sfsdfsd((1,2))。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>字符串&#xff0c;表示记录仪中的数据。</p> 
<p>如&#xff1a;ferga13fdsf3(100,200)f2r3rfasf(300,400)</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>字符串&#xff0c;表示最远足迹到达的坐标。</p> 
<p>如&#xff1a; (300,400)</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">ferg(3,10)a13fdsf3(3,4)f2r3rfasf(5,10)</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">(5,10)</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">记录仪中的合法坐标有3个&#xff1a; (3,10)&#xff0c; (3,4)&#xff0c; (5,10)&#xff0c;其中(5,10)是相距总部最远的坐标&#xff0c; 输出(5,10)。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">asfefaweawfaw(0,1)fe</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">(0,0)</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">记录仪中的坐标都不合法&#xff0c;输出总部坐标&#xff08;0,0&#xff09;。</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题首先需要解析出输入字符串s中&#xff0c;括号对中的坐标&#xff0c;我的策略是&#xff0c;遍历输入的字符串&#xff1a;</p> 
<ul><li>如果遇到 &#39;(&#39; 字符&#xff0c;则记录该字符的索引 i &#43; 1 为L&#xff0c;即坐标的左边界&#xff08;左闭&#xff09;</li><li>如果遇到 &#39;)&#39; 字符&#xff0c;则取该字符的所有 i 为R&#xff0c;即坐标的右边界&#xff08;右开&#xff09;&#xff0c;然后截取s.substr(L,R)&#xff0c;取得括号对中的内容</li></ul> 
<p>取得括号对中的内容后&#xff0c;首先需要按照“,”分隔&#xff0c;得到两个子串x&#xff0c;y&#xff0c;此时根据题意&#xff0c;需要对x&#xff0c;y做校验&#xff1a;</p> 
<ul><li>题目说(01,1)、(1,01)为非法坐标&#xff0c;即x&#xff0c;y不能含有前导0</li><li>题目说0&lt;x&lt;1000&#xff0c;0&lt;y&lt;1000&#xff0c;因此x&#xff0c;y转为整型后&#xff0c;只能处于0~1000范围之内&#xff0c;不含0&#xff0c;1000</li></ul> 
<p>如果校验合法&#xff0c;则此时计算坐标到起点的距离为 far &#61; x * x &#43; y * y&#xff0c;如果far &gt; maxFar&#xff0c;则更新最远坐标信息&#xff0c;否则不更新。</p> 
<blockquote> 
 <p>若两个座标的相对总部的距离相同&#xff0c;则第一次到达的坐标为最远的足迹。</p> 
</blockquote> 
<p>对于相同最远距离的坐标&#xff0c;由于后出现的最远距离坐标不满足far &gt; maxFar&#xff0c;因此不会覆盖前面最远坐标信息。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    System.out.println(getResult(sc.nextLine()));
  }

  public static String getResult(String s) {
    int maxFar &#61; 0;
    String ans &#61; &#34;(0,0)&#34;;

    int l &#61; 0;
    for (int i &#61; 0; i &lt; s.length(); i&#43;&#43;) {
      char c &#61; s.charAt(i);

      if (c &#61;&#61; &#39;(&#39;) {
        l &#61; i &#43; 1;
      } else if (c &#61;&#61; &#39;)&#39;) {
        String[] pos &#61; s.substring(l, i).split(&#34;,&#34;);

        if (pos[0].charAt(0) &#61;&#61; &#39;0&#39; || pos[1].charAt(0) &#61;&#61; &#39;0&#39;) continue;

        int x &#61; Integer.parseInt(pos[0]);
        int y &#61; Integer.parseInt(pos[1]);

        if (x &lt;&#61; 0 || x &gt;&#61; 1000 || y &lt;&#61; 0 || y &gt;&#61; 1000) continue;

        int far &#61; x * x &#43; y * y;
        if (far &gt; maxFar) {
          maxFar &#61; far;
          ans &#61; &#34;(&#34; &#43; x &#43; &#34;,&#34; &#43; y &#43; &#34;)&#34;;
        }
      }
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  console.log(getResult(line));
});

function getResult(s) {
  let maxFar &#61; 0;
  let ans &#61; &#34;(0,0)&#34;;

  let l &#61; 0;
  for (let i &#61; 0; i &lt; s.length; i&#43;&#43;) {
    if (s[i] &#61;&#61; &#34;(&#34;) {
      l &#61; i &#43; 1;
    } else if (s[i] &#61;&#61; &#34;)&#34;) {
      let [x, y] &#61; s.slice(l, i).split(&#34;,&#34;);

      if (x[0] &#61;&#61; &#34;0&#34; || y[0] &#61;&#61; &#34;0&#34;) continue;

      x &#61; parseInt(x);
      y &#61; parseInt(y);

      if (x &lt;&#61; 0 || x &gt;&#61; 1000 || y &lt;&#61; 0 || y &gt;&#61; 1000) continue;

      const far &#61; x * x &#43; y * y;
      if (far &gt; maxFar) {
        maxFar &#61; far;
        ans &#61; &#96;(${x},${y})&#96;;
      }
    }
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()


# 算法入口
def getResult():
    maxFar &#61; 0
    ans &#61; &#34;(0,0)&#34;

    l &#61; 0
    for i in range(len(s)):
        if s[i] &#61;&#61; &#39;(&#39;:
            l &#61; i &#43; 1
        elif s[i] &#61;&#61; &#34;)&#34;:
            x, y &#61; s[l:i].split(&#34;,&#34;)

            if x[0] &#61;&#61; &#39;0&#39; or y[0] &#61;&#61; &#39;0&#39;:
                continue

            x &#61; int(x)
            y &#61; int(y)

            if x &lt;&#61; 0 or x &gt;&#61; 1000 or y &lt;&#61; 0 or y &gt;&#61; 1000:
                continue

            far &#61; x ** 2 &#43; y ** 2
            if far &gt; maxFar:
                maxFar &#61; far
                ans &#61; f&#34;({x},{y})&#34;

    return ans


# 调用算法
print(getResult())</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>